<template>
	<xn-form-container
		:title="formData.username + '设置围栏'"
		:width="550"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="用户名称：" name="username">
				<a-input v-model:value="formData.username" placeholder="请输入用户名称" disabled allow-clear />
			</a-form-item>
			<a-form-item label="手机号：" name="phone">
				<a-input v-model:value="formData.phone" placeholder="请输入手机号" disabled allow-clear />
			</a-form-item>
			<a-form-item label="公司名称：" name="company_name">
				<a-input v-model:value="formData.company_name" placeholder="请输入公司名称" disabled allow-clear />
			</a-form-item>
			<a-form-item label="围栏：" name="fence_id">
				<a-select
					:options="fenceList"
					mode="multiple"
					v-model:value="formData.fence_id"
					show-search
					placeholder="请选择围栏"
				/>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button class="xn-mr8" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup>
	import { required, rules } from '@/utils/formRules'
	import smCrypto from '@/utils/smCrypto'
	import dayjs from 'dayjs'
	import api from './api'
	import fenceApi from '@/views/moxing/fence/api'
	import userFenceApi from '@/views/moxing/userFenceList/api'
	// 默认是关闭状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})

	const fenceList = ref([])

	const total = ref(0)
	// 打开抽屉
	const onOpen = (records) => {
		formData.value = records
		fenceApi.modulePage({ pageNow: 1, pagesize: 9999 }).then(({ record }) => {
			fenceList.value = record.map((item) => ({ label: item.fence_name, value: item.fence_id }))
			let params = {
				user_id: records.id
			}
			api.findFenceByUser(params).then((res) => {
				formData.value.fence_id = res.map((item) => item.fence_id)
				visible.value = true
			})
		})
	}

	const loadFenceList = () => {
		fenceApi.modulePage({ pageNow: 1, pagesize: 9999 }).then((res) => {
			total.value = res.total
		})
	}
	loadFenceList()
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		visible.value = false
	}
	// 图标选择器回调
	const iconCallBack = (value) => {
		if (value) {
			formRef.value.clearValidate('icon')
		}
		formData.value.icon = value
	}

	// 默认要校验的
	const formRules = {
		username: [required('请输入客户名称')],
		phone: { required: true, ...rules.phone },
		company_name: [required('请输入公司名称')],
		fence_id: [required('请选择围栏')]
	}

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				formData.value.fence_id.map((item, index) => {
					const fence_name = fenceList.value.find((c) => c.value == item)[0].label
					userFenceApi.submitForm({})
				})
				const json = {}
				api
					.submitForm(
						{
							...formData.value,
							id: formData.value.id || index,
							...(formData.value.id
								? {}
								: {
										create_time: dayjs().format('YYYY-MM-DD HH:mm:ss')
								  }),
							modified_time: dayjs().format('YYYY-MM-DD HH:mm:ss')
						},
						formData.value.id
					)
					.then(() => {
						onClose()
						emit('successful')
					})
			})
			.catch(() => {})
	}

	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
